#include "MainForm.h"
#include "QmlClasses/QmlClassRegistrator.h"

#include <QtGui/QApplication>
#include <QtDeclarative>


//#define DEBUG_TREE
#ifdef DEBUG_TREE

#include <iostream>
#include <vector>
#include <cstdlib>
#include "Tree/structs.h"
#include "Tree/Root.h"
#include "Tree/Node.h"

#include <QString>

using namespace std;

void test();
#endif

int main(
        #ifndef DEBUG_TREE
          int argc, char *argv[]
        #endif
        )
{
#ifdef DEBUG_TREE
    test();
    return 0;
#else
    QApplication app(argc, argv);

    QmlClassRegistrator::registerTypes("Lab03", 1, 0);
    MainForm form(QUrl("qml/Lab03View.qml"));
    QObject::connect(&form, SIGNAL(closed()), &app, SLOT(quit()));
    form.show();

    return app.exec();
#endif
}

#ifdef DEBUG_TREE
void test()
{
    Node::fail.invalidate();
    Node::fail.setKey(0);
    Elem elem;

    Root root;
    Node* root_ptr = new Node(root);
    root.set_root (NULL, root_ptr);

    QString str;
    // Добавление.
    for (int i=1; i<1000; i++) {
        //elem.key = rand() % 1000000;
        elem.setKey(rand() % 1000);
        str = QString("%1 data").arg(i);
        //elem.data = str;
        elem.setData(str);
        root.get_root()->tree_insert(elem);
        //root.get_root()->dump();
    }

    cout << "finished inserting elements\n";
    root.get_root()->dump();

    // Поиск.
    Node * last;
    for (int i=1; i<1000; i++){
        Elem target;
        target.setKey(rand() % 1000);
        str = QString("%1 data").arg(i);
        //elem.data = str;
        target.setData(str);
        Elem tst = root.get_root()->search(target, last);
       // cout << "Key   "<< tst.key() /*<< "Data  " << tst.data()*/<< endl;
    }
    // Удаление.
    for (int i=10000; i>0; i--) {
            Elem target;
            //target.key = rand() % 1000000;
            target.setKey(rand() % 1000);

            root.get_root()->delete_element (target);
            //root.get_root()->dump();
        }

    root.get_root()->dump();
}
#endif
